<?php
    $_helper = $this->helper('catalog/output');
    $helper_richsnippets = $this->helper('porto/richsnippets');
    $store = Mage::app()->getStore();
    $code  = $store->getCode();
?>
<?php $_product = $this->getProduct(); ?>
<?php
    $product = Mage::getModel('catalog/product')->load($_product->getId());
    $product_type = $_product->getTypeId();
    $enabled_richsnippets = ($helper_richsnippets->isEnabled() && $product_type !== "grouped");
    $price_properties = "";
    
    if($enabled_richsnippets){
        $price_properties = $helper_richsnippets->getPriceProperties($_product);
    }
    
    $enable_addtocart_sticky = Mage::getStoreConfig("porto_settings/product_view/enable_addtocart_sticky",$code);
    $move_product_tab = Mage::getStoreConfig("porto_settings/product_view/move_product_tab",$code);
    $sticky_header = "";
    if(!Mage::getStoreConfig("porto_settings/header/sticky_header", $code))
        $sticky_header = "no-sticky-header";
?>
<script type="text/javascript">
    var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
</script>
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
<div class="product-view <?php if($move_product_tab): ?>moved-tab<?php endif;?>" <?php if($enabled_richsnippets): ?><?php echo $helper_richsnippets->getProductItemscope(); ?><?php endif; ?>>
    <div class="product-essential">
    <form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
        <?php echo $this->getBlockHtml('formkey') ?>
        <div class="no-display">
            <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
            <input type="hidden" name="related_product" id="related-products-field" value="" />
        </div>
<?php
if($enable_addtocart_sticky){
?>
        <div class="sticky-product hide <?php echo $sticky_header; ?>">
            <div class="container">
<?php
    $aspect_ratio = Mage::getStoreConfig("porto_settings/product_view/aspect_ratio", $code);
    $ratio_width = 600;
    $ratio_height = 600;
    if(Mage::getStoreConfig("porto_settings/product_view/ratio_width", $code))
        $ratio_width = Mage::getStoreConfig("porto_settings/product_view/ratio_width", $code);
    if(!$aspect_ratio){
        if(Mage::getStoreConfig("porto_settings/product_view/ratio_height", $code))
            $ratio_height = Mage::getStoreConfig("porto_settings/product_view/ratio_height", $code);
    }
    $ratio = $ratio_height / $ratio_width;
    $ratio_width = 50;
    $ratio_height = $ratio_width * $ratio;
?>
                <div class="sticky-image">
                    <img src="<?php if($aspect_ratio):?><?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(50);?><?php else: ?><?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($ratio_width,$ratio_height); ?><?php endif; ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
                </div>
                <div class="sticky-detail">
                    <div class="product-name-area">
                        <h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
                        <?php echo $this->getPriceHtml($_product, false, '_clone') ?>
                    </div>
                    <?php 
                    if (Mage::helper('catalog')->isModuleEnabled('Mage_Review')):
                    ?>
                    <?php 
                        $review_html = $this->getReviewsSummaryHtml($product, 'short');
                        if($review_html){
                            echo $review_html;
                        }else{
                    ?>
                        <div class="ratings">
                            <div class="rating-box">
                                <div class="rating" style="width:0"></div>
                            </div>
                        </div>
                    <?php                            
                        }
                    ?>
                    <?php else: ?>
                        <div class="ratings">
                            <div class="rating-box">
                                <div class="rating" style="width:0"></div>
                            </div>
                        </div>
                    <?php endif; ?>
                    <?php if($enabled_richsnippets): ?>
                    <?php if ($this->getReviewsCount()): ?>
                        <span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
                            <meta itemprop="ratingValue" content="<?php echo $this->getRatingSummary(); ?>"/>
                            <meta itemprop="reviewCount" content="<?php echo $this->getReviewsCount(); ?>" />
                            <meta itemprop="bestRating" content="100"/>
                            <meta itemprop="worstRating" content="0"/>
                        </span>
                    <?php endif; ?>
                    <?php endif; ?>
					<?php if ($_product->isAvailable()): ?>
						<p class="availability in-stock"><span>(<?php echo $this->__('In stock') ?>)</span></p>
					<?php else: ?>
						<p class="availability out-of-stock"><span>(<?php echo $this->__('Out of stock') ?>)</span></p>
					<?php endif; ?>
                </div>
                <?php if($_product->isSaleable()): ?>
                <?php $buttonTitle = $this->__('Add to Cart'); ?>
                    <div class="add-to-cart">
                        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><i class="icon-cart"></i><?php echo $buttonTitle ?></span></span></button>
                    </div>
                <?php endif; ?>
            </div>
        </div>
<?php
}
?>
        <div class="row">
            <div class="product-img-box col-sm-<?php echo Mage::getStoreConfig("porto_settings/product_view/product_image_size", $code); ?> <?php echo Mage::getStoreConfig("porto_settings/product_view/thumbnail_image_type", $code); ?>">
                <?php echo $this->getChildHtml('media') ?>
            </div>
            <div class="product-shop col-sm-<?php echo 12-Mage::getStoreConfig("porto_settings/product_view/product_image_size", $code); ?>">
            <?php
                if (Mage::getStoreConfig("porto_settings/product_view/prev_next",$code)){
            ?>
                <div class="prev-next-products">
                    <?php $_prev = Mage::helper('porto')->getPreviousProduct(); ?>
                    <?php $_next = Mage::helper('porto')->getNextProduct(); ?>
                     
                    <?php if($_prev): ?>
                    <div class="product-nav product-prev">
                        <a href="<?php echo $_prev->getProductUrl();?>" title="<?php echo $this->__("Previous Product"); ?>">
                            <?php echo Mage::getStoreConfig("porto_settings/product_view/prev_text",$code); ?>
                        </a>
                        <div class="product-pop theme-border-color">
                            <?php
                            $image_src = $this->helper('catalog/image')->init($_prev, 'image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(80);
                            ?>
                            <img class="product-image" src="<?php echo $image_src; ?>" alt="<?php echo $this->__('Previous');?>"/>
                            <h3 class="product-name"><?php echo $_helper->productAttribute($_prev, $_prev->getName(), 'name') ?></h3>
                        </div>
                    </div>
                    <?php endif; ?>
                    <?php if($_next): ?>
                    <div class="product-nav product-next">
                        <a class="product-next" href="<?php echo $_next->getProductUrl();?>" title="<?php echo $this->__("Next Product"); ?>"><?php echo Mage::getStoreConfig("porto_settings/product_view/next_text",$code); ?></a>
                        <div class="product-pop theme-border-color">
                            <?php
                            $image_src = $this->helper('catalog/image')->init($_next, 'image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(80);
                            ?>
                            <img class="product-image" src="<?php echo $image_src; ?>" alt="<?php echo $this->__('Previous');?>"/>
                            <h3 class="product-name"><?php echo $_helper->productAttribute($_next, $_next->getName(), 'name') ?></h3>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            <?php
                }
            ?>
                <div class="product-name">
                    <h1 <?php if($enabled_richsnippets): ?>itemprop="name"<?php endif; ?>><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
                </div>
                <?php if($enabled_richsnippets): ?>
                <?php
                    $storeId = Mage::app()->getStore()->getId();
                    $summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)->load($_product->getId());
                ?>
                <?php if ($summaryData['reviews_count']): ?>
                    <span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating" class="no-display">
                        <span itemprop="ratingValue"><?php echo $summaryData['rating_summary']; ?></span>
                        <span itemprop="reviewCount"><?php echo $summaryData['reviews_count']; ?></span>
                        <span itemprop="bestRating">100</span>
                        <span itemprop="worstRating">0</span>
                    </span>
                <?php endif; ?>
                <?php endif; ?>

                <?php echo $this->getReviewsSummaryHtml($_product, false, true)?>
                <?php if ($_product->getShortDescription()):?>
                    <div class="short-description">
                        <h2><?php echo $this->__('Quick Overview') ?></h2>
                        <div class="std" <?php if($enabled_richsnippets): ?>itemprop="description"<?php endif; ?>><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
                    </div>
                <?php endif;?>
                <div class="product-info">
                    <div <?php if($enabled_richsnippets) echo $helper_richsnippets->getOfferItemscope(); ?>>
                        <?php echo $this->getChildHtml('product_type_data'); ?>
                        <?php echo $this->getTierPriceHtml(); ?>
                        <?php echo $price_properties; ?>
                    </div>
                    <?php echo $this->getChildHtml('extrahint') ?>
                    <?php if ($this->canEmailToFriend()): ?>
                        <p class="email-friend"><a href="<?php echo $this->helper('catalog/product')->getEmailToFriendUrl($_product) ?>"><?php echo $this->__('Email to a Friend') ?></a></p>
                    <?php endif; ?>
                    <?php if($sku = $_product->getSku()): ?>
                        <?php if($enabled_richsnippets): ?><meta itemprop="productID" content="sku:<?php echo $sku; ?>" /><?php endif; ?>
                    <?php endif; ?>
                    <?php echo $this->getChildHtml('alert_urls') ?>
                    <?php echo $this->getChildHtml('other');?>
                </div>
                <?php if (!$this->hasOptions()):?>
                    <div class="clearer"></div>
                    <div class="add-to-box">
                        <?php if($_product->isSaleable()): ?>
                            <?php echo $this->getChildHtml('addtocart') ?>
                            <?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php echo $this->getChildHtml('addto') ?>
                    </div>
                    <?php echo $this->getChildHtml('extra_buttons') ?>
                <?php elseif (!$_product->isSaleable()): ?>
                    <div class="clearer"></div>
                    <div class="add-to-box">
                        <?php echo $this->getChildHtml('addto') ?>
                    </div>
                <?php endif; ?>

                <?php if ($_product->isSaleable() && $this->hasOptions()):?>
                    <?php echo $this->getChildChildHtml('container1', '', true, true) ?>
                <?php endif;?>
                <div class="clearer"></div>
                <?php echo $this->getChildHtml('addthis_sharetool'); ?>
            </div>

        </div>
        <?php if ($_product->isSaleable() && $this->hasOptions()):?>
            <?php echo $this->getChildChildHtml('container2', '', true, true) ?>
        <?php endif;?>
    </form>
<?php if(Mage::getStoreConfig('ajaxcart/addtocart/enableproduct') && (Mage::app()->getFrontController()->getRequest()->getModuleName() == "catalog") || (Mage::app()->getFrontController()->getRequest()->getModuleName() == "quickview")): ?>
    <script type="text/javascript">
    //<![CDATA[
        var productAddToCartForm = new VarienForm('product_addtocart_form');
        productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                   form.action = url;
                }
                var e = null;
                if (!url) {
                    url = jQuery('#product_addtocart_form').attr('action');
                }
                if(url.indexOf("wishlist/index/cart") != -1){
                    url = url.replace("wishlist/index/cart","ajaxcart/index/add"); // New Code
                } else {
                    url = url.replace("checkout/cart","ajaxcart/index"); // New Code
                }
                url = url.replace("http://", "//");
                url = url.replace("https://", "//");
                var data = jQuery('#product_addtocart_form').serialize();
                data += '&isAjax=1';
                var is_quickview = false;
                if(jQuery("body").hasClass("quickview-index-view")){
                    is_quickview = true;
                }
                if(is_quickview)
                    window.parent.jQuery("#loading-mask").show();
                else
                    jQuery('#loading-mask').show();
                try {
                    jQuery.ajax({
                        url : url,
                        dataType : 'json',
                        type : 'post',
                        data : data,
                        success : function(data) {
                            if(is_quickview)
                                window.parent.jQuery('#loading-mask').hide();
                            else
                                jQuery('#loading-mask').hide();
                            if(data.status == 'ERROR'){
                                if(is_quickview)
                                    window.parent.alert(data.message.replace("<br/>",""));
                                else
                                    alert(data.message.replace("<br/>",""));
                            }else{
                                if(is_quickview){
                                    if(window.parent.jQuery('.header-container .mini-cart')){
                                        window.parent.jQuery('.header-container .mini-cart').replaceWith(data.toplink);
                                    }
                                    if(window.parent.jQuery('.fixed-header .mini-cart')){
                                        window.parent.jQuery('.fixed-header .mini-cart').replaceWith(data.toplink);
                                    }
									if(window.parent.jQuery('.sticky-header .mini-cart')){
										window.parent.jQuery('.sticky-header .mini-cart').replaceWith(data.toplink);
									}
                                    if(window.parent.jQuery('.col-right .block.block-cart')){
                                        window.parent.jQuery('.col-right .block.block-cart').replaceWith(data.cart_sidebar);
                                    }
                                    window.parent.jQuery('#after-loading-success-message #success-message-container .msg-box').html(data.message);
                                    <?php $autoclose = Mage::getStoreConfig('ajaxcart/addtocart/autoclose', $code);?>
                                    ajaxcart_sec = <?php echo ($autoclose && is_numeric($autoclose))?$autoclose:5; ?>;
                                    timer_sec = ajaxcart_sec;
                                    window.parent.jQuery('#after-loading-success-message').fadeIn(200);
                                    ajaxcart_timer = setInterval(function(){
                                        timer_sec --;
                                        window.parent.jQuery('#after-loading-success-message .timer').html(timer_sec);
                                    },1000)
                                    setTimeout(function(){
                                        window.parent.jQuery('#after-loading-success-message').fadeOut(200);
                                        clearTimeout(ajaxcart_timer);
                                        setTimeout(function(){
                                            window.parent.jQuery('#after-loading-success-message .timer').html(ajaxcart_sec);
                                        }, 1000);
                                    },ajaxcart_sec*1000);
                                }
                                else {
                                    if(jQuery('.header-container .mini-cart')){
                                        jQuery('.header-container .mini-cart').replaceWith(data.toplink);
                                    }
                                    if(jQuery('.fixed-header .mini-cart')){
                                        jQuery('.fixed-header .mini-cart').replaceWith(data.toplink);
                                    }
									if(jQuery('.sticky-header .mini-cart')){
										jQuery('.sticky-header .mini-cart').replaceWith(data.toplink);
									}
                                    if(jQuery('.col-right .block.block-cart')){
                                        jQuery('.col-right .block.block-cart').replaceWith(data.cart_sidebar);
                                    }
                                    jQuery('#after-loading-success-message #success-message-container .msg-box').html(data.message);
                                    <?php $autoclose = Mage::getStoreConfig('ajaxcart/addtocart/autoclose', $code);?>
                                    ajaxcart_sec = <?php echo ($autoclose && is_numeric($autoclose))?$autoclose:5; ?>;
                                    timer_sec = ajaxcart_sec;
                                    jQuery('#after-loading-success-message').fadeIn(200);
                                    ajaxcart_timer = setInterval(function(){
                                        timer_sec --;
                                        jQuery('#after-loading-success-message .timer').html(timer_sec);
                                    },1000)
                                    setTimeout(function(){
                                        jQuery('#after-loading-success-message').fadeOut(200);
                                        clearTimeout(ajaxcart_timer);
                                        setTimeout(function(){
                                            jQuery('#after-loading-success-message .timer').html(ajaxcart_sec);
                                        }, 1000);
                                    },ajaxcart_sec*1000);
                                }
                            }
                        }
                    });
                } catch (e) {
                }                
                this.form.action = oldUrl;
                if (e) {
                    throw e;
                }
            } else {
                if(jQuery('#product-options-wrapper'))
                    jQuery('#product-options-wrapper').scrollToMe();
            }
        }.bind(productAddToCartForm);
    //]]>
    </script>
<?php else : ?>
    <script type="text/javascript">
    //<![CDATA[
        var productAddToCartForm = new VarienForm('product_addtocart_form');
        productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                   form.action = url;
                }
                var e = null;
                try {
                    this.form.submit();
                } catch (e) {
                }
                this.form.action = oldUrl;
                if (e) {
                    throw e;
                }

                if (button && button != 'undefined') {
                    button.disabled = true;
                }
            } else {
                if(jQuery('#product-options-wrapper'))
                    jQuery('#product-options-wrapper').scrollToMe();
            }
        }.bind(productAddToCartForm);
        //]]>
        </script>
        <?php endif; ?>
        <script type="text/javascript">
        productAddToCartForm.submitLight = function(button, url){
            if(this.validator) {
                var nv = Validation.methods;
                delete Validation.methods['required-entry'];
                delete Validation.methods['validate-one-required'];
                delete Validation.methods['validate-one-required-by-name'];
                // Remove custom datetime validators
                for (var methodName in Validation.methods) {
                    if (methodName.match(/^validate-datetime-.*/i)) {
                        delete Validation.methods[methodName];
                    }
                }

                if (this.validator.validate()) {
                    if (url) {
                        this.form.action = url;
                    }
                    this.form.submit();
                }
                Object.extend(Validation.methods, nv);
            } else {
                if(jQuery('#product-options-wrapper'))
                    jQuery('#product-options-wrapper').scrollToMe();
            }
        }.bind(productAddToCartForm);
    </script>
    </div>

    <div class="product-collateral">
        <?php echo $this->getChildHtml('product_additional_data'); ?>
    </div>
    <?php echo $this->getChildHtml('upsell_products'); ?>
</div>
<script type="text/javascript">
    jQuery(function($){
        $("body.quickview-index-view .no-rating a, body.quickview-index-view .ratings a").off('click').on("click",function(e){
            window.parent.location.href = $(this).attr("href");
            window.parent.jQuery.fancybox.close();
        });
    <?php
    if($enable_addtocart_sticky){
    ?>
        p_scrolled = false;
        $(window).scroll(function(){
            if(400<$(window).scrollTop() && !p_scrolled){
                $('.sticky-product').removeClass("hide");
                p_scrolled = true;
            }
            if(400>=$(window).scrollTop() && p_scrolled){
                $('.sticky-product').addClass("hide");
                p_scrolled = false;
            }
        });
    <?php
    }
    ?>
    <?php
    if($move_product_tab){
    ?>
        $(".product-info").before($(".product-tabs"));
    <?php
    }
    ?>
    });
</script>